package com.htht.smartschool.util.login;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;

public class SerializeUtil {
	
	static String charset="ASCII";
	public static String serializeStr(Object object) {
		byte[] arr=serialize(object);
		String str="";
		try {
			str= new String(arr,charset);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
		baos = new ByteArrayOutputStream();
		oos = new ObjectOutputStream(baos);
		oos.writeObject(object);
		byte[] bytes = baos.toByteArray();
		return bytes;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Object unserialize(String str) {
		byte [] arr=null;
		try {
			arr=str.getBytes(charset);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return unserialize(arr);
	}
	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			//�����л�
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}